package keeto.com.vn.classified.task;

import java.io.IOException;

import keeto.com.vn.classified.core.ClassifiedCore;
import keeto.com.vn.classified.core.ClassifiedListener;
import keeto.com.vn.classified.network.IProtocol;
import keeto.com.vn.classified.request.CommentRequest;
import keeto.com.vn.classified.response.CommentResponse;

import org.apache.http.client.ClientProtocolException;
import org.json.JSONException;

public class TaskComment extends AbstractTask {

	String itemId;
	String message;
	String userName;
	int rating;
	String type;
	String titleMsg;
	boolean isCommented;
	String timeComment;

	public TaskComment(ClassifiedListener listeners, ClassifiedCore core, String itemId,String titleMessage,
			String message,String userName,int rating,String type , boolean isComment, String time) {
		super(listeners, core);
		this.itemId = itemId;
		this.message = message;
		this.userName = userName;
		this.rating = rating;
		this.type = type;
		this.titleMsg = titleMessage;
		this.isCommented = isComment;
		this.timeComment = time;
	}

	@Override
	public void run() throws ClientProtocolException, JSONException,
			IOException {
		CommentRequest request = new CommentRequest();
		request.setAuthkey(authKey);
		request.setItemId(itemId);
		request.setMessage(message);
		request.setUserName(userName);
		request.setRating(rating);
		request.setType(type);
		request.setMessageTitle(titleMsg);
		request.setComment(isCommented);
		request.setTimeComment(timeComment);
		
		CommentResponse response = json.comment(request);
		for (ClassifiedListener listener : listeners) {
			listener.finishIAComment(response.getReturnCode(), response.getResultMsg());
		}
	}

	@Override
	public void timeoutCallback() {
		for (ClassifiedListener l : core.copyAndReconstructListeners(listener)) {
			l.finishIAComment(IProtocol.STATUS_CODE_CONNECTION_TIMEOUT_502, null);
		}
	}

}
